program Polynomial;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  pelement = ^element;
  element = record
    a: integer;
    k: integer;
    next: pelement;
  end;

var
  head1, head2, head: pelement;
  fin: textFile;
  num1, num2, n, m, i: integer;



procedure Add (var head: pelement; num1, num2: integer);
  var tmp: pelement;
  begin
    tmp:= head;
    new (head);
    head^.next:= tmp;
    head^.a:=num1;
    head^.k:=num2
  end;



procedure AddToEnd (var head: pelement; num1, num2: integer);
  var current: pelement;
  begin
    if (head <> nil) then
    begin
      current:= head;
      while (current^.next <> nil) do
        current:= current^.next;
      new (current^.next);
      current^.next^.a:=num1;
      current^.next^.k:=num2;
      current^.next^.next:=nil;
    end else begin
      Add (head, num1, num2);
    end;
  end;

procedure PrintList (head: pelement);
  var current: pelement;
  begin
    current:= head;
    while (current<>nil) do
    begin
      writeln (current^.a, '   ', current^.k);
      current:= current^.next;
    end;
  end;

procedure Summ (var head1, head2, head: pelement);
  var current1, current2, current: pelement; flag: boolean;
  begin
    new (head);
    current:= head;
    current1:= head1;
    current2:= head2;
    while (current1 <> nil) or (current2 <> nil) do
    begin
    while (current1 <> nil) or (current2 <> nil) do
    begin
      flag:=false;
      if (current1 = nil) then
      begin
        current^.a:= current1^.a + current2^.a;
        current^.k:= current1^.k;
        current1:= current1^.next;
        current2:= current2^.next;
        if (current1 <> nil) or (current2 <> nil) then
        begin
          new (current.next);
          current:= current^.next;
        end;  
      end;
      if (current2 = nil) then
      begin
        current^.a:= current1^.a;
        current^.k:= current1^.k;
        current1:= current1^.next;
        if (current1 <> nil) or (current2 <> nil) then
        begin
          new (current.next);
          current:= current^.next;
        end;
        flag:=true;
      end;
      if (current1 <> nil) and (current2 <> nil) then
      begin
      if (current1^.k > current2^.k) then
      begin
        current^.a:= current1^.a;
        current^.k:= current1^.k;
        current1:= current1^.next;
        if (current1 <> nil) or (current2 <> nil) then
        begin
          new (current.next);
          current:= current^.next;
        end;
        flag:=true;
      end;
      if (flag) then
        break;
      if (current1^.k = current2^.k) then
      begin
        current^.a:= current1^.a + current2^.a;
        current^.k:= current1^.k;
        current1:= current1^.next;
        current2:= current2^.next;
        if (current1 <> nil) or (current2 <> nil) then
        begin
          new (current.next);
          current:= current^.next;
        end;
        flag:=true;
      end;
      if (flag) then
        break;
      if (current1^.k < current2^.k) then
      begin
        current^.a:= current2^.a;
        current^.k:= current2^.k;
        current2:= current2^.next;
        if (current1 <> nil) or (current2 <> nil) then
        begin
          new (current.next);
          current:= current^.next;
        end;
        flag:=true;
      end;
      if (flag) then
        break;
      end;
    end;
    end;
  end;

begin
  assignFile (fin, 'input.txt');
  reset (fin);
  read (fin, n);
  for i:= 1 to n do
    begin
      read (fin, num1);
      read (fin, num2);
      AddToEnd (head1, num1, num2);
    end;
  read (fin, m);
  for i:= 1 to m do
    begin
      read (fin, num1);
      read (fin, num2);
      AddToEnd (head2, num1, num2);
    end;
  printlist (head1);
   writeln (' ');
  printlist (head2);
  writeln (' ');
  Summ (head1, head2, head);
  closeFile(fin);
  printlist (head);
  readln;
end.
